<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Hexo, NexT" />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="蛮力算法子集12345678910111213141516171819#include&amp;lt;iostream&amp;gt;using namespace std;#define MAX 1000int main() &amp;#123;	int n;	int a[MAX];	int tol;//子集的个数	n=4;	for(int i=0; i&amp;lt;n; i++) &amp;#123;		cin&amp;gt;&amp;gt;a">
<meta property="og:type" content="article">
<meta property="og:title" content="几个经典算法">
<meta property="og:url" content="http://yoursite.com/2017/10/20/几个经典算法/index.html">
<meta property="og:site_name" content="lewyu">
<meta property="og:description" content="蛮力算法子集12345678910111213141516171819#include&amp;lt;iostream&amp;gt;using namespace std;#define MAX 1000int main() &amp;#123;	int n;	int a[MAX];	int tol;//子集的个数	n=4;	for(int i=0; i&amp;lt;n; i++) &amp;#123;		cin&amp;gt;&amp;gt;a">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-10-20T14:37:22.821Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="几个经典算法">
<meta name="twitter:description" content="蛮力算法子集12345678910111213141516171819#include&amp;lt;iostream&amp;gt;using namespace std;#define MAX 1000int main() &amp;#123;	int n;	int a[MAX];	int tol;//子集的个数	n=4;	for(int i=0; i&amp;lt;n; i++) &amp;#123;		cin&amp;gt;&amp;gt;a">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2017/10/20/几个经典算法/"/>





  <title>几个经典算法 | lewyu</title>
  














</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">lewyu</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2017/10/20/几个经典算法/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="lewyu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/touxiang.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="lewyu">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">几个经典算法</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-10-20T22:09:44+08:00">
                2017-10-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <hr>
<h2 id="蛮力算法"><a href="#蛮力算法" class="headerlink" title="蛮力算法"></a>蛮力算法</h2><p>子集<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX 1000</span></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">	<span class="keyword">int</span> n;</div><div class="line">	<span class="keyword">int</span> a[MAX];</div><div class="line">	<span class="keyword">int</span> tol;<span class="comment">//子集的个数</span></div><div class="line">	n=<span class="number">4</span>;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++) &#123;</div><div class="line">		<span class="built_in">cin</span>&gt;&gt;a[i];</div><div class="line">	&#125;</div><div class="line">	tol=(<span class="number">1</span>&lt;&lt;n)<span class="number">-1</span>; <span class="comment">//子集的个数</span></div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;=tol; i++) &#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;n; j++)</div><div class="line">			<span class="keyword">if</span>(i&amp;(i&lt;&lt;j)) <span class="built_in">cout</span>&lt;&lt;a[j];  <span class="comment">//i&lt;&lt;j相当于对每一个进行标记 </span></div><div class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="string">" "</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="递归算法"><a href="#递归算法" class="headerlink" title="递归算法"></a>递归算法</h2><p>1.输出全排列<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Perm</span><span class="params">(<span class="keyword">int</span> <span class="built_in">list</span>[],<span class="keyword">int</span> k,<span class="keyword">int</span> m)</span> </span>&#123;</div><div class="line">	<span class="keyword">if</span>(k==m) &#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;=m; i++)</div><div class="line">			<span class="built_in">cout</span>&lt;&lt;<span class="built_in">list</span>[i]&lt;&lt;<span class="string">"　"</span>;</div><div class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	&#125; <span class="keyword">else</span> &#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=k; j&lt;=m; j++) &#123;</div><div class="line">			swap(<span class="built_in">list</span>[k],<span class="built_in">list</span>[j]);</div><div class="line">			Perm(<span class="built_in">list</span>,k+<span class="number">1</span>,m);</div><div class="line">			swap(<span class="built_in">list</span>[k],<span class="built_in">list</span>[j]);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line"></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">	<span class="keyword">int</span> <span class="built_in">list</span>[]= &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;;</div><div class="line">	Perm(<span class="built_in">list</span>,<span class="number">0</span>,<span class="number">3</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>//正整数n的划分算法<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">split</span><span class="params">(<span class="keyword">int</span> n,<span class="keyword">int</span> m)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">if</span>(n==<span class="number">1</span>||m==<span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</div><div class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (n&lt;m) <span class="keyword">return</span> split(n,n);</div><div class="line">	<span class="keyword">else</span> <span class="keyword">if</span>(n==m) <span class="keyword">return</span> split(n,n<span class="number">-1</span>)+<span class="number">1</span>;</div><div class="line">	<span class="keyword">else</span> <span class="keyword">return</span> split(n,m<span class="number">-1</span>)+split(n-m,m);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> n;</div><div class="line">	<span class="keyword">while</span> (<span class="built_in">cin</span>&gt;&gt;n)</div><div class="line">		<span class="built_in">cout</span>&lt;&lt;split(n, n)&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line">	<span class="comment">//对一个段进行递归调用</span></div><div class="line">	<span class="keyword">return</span> select(j+<span class="number">1</span>, right, k-j+left<span class="number">-1</span>);</div><div class="line">	<span class="keyword">else</span> <span class="keyword">return</span> select(left, j<span class="number">-1</span>, k);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> n, k;</div><div class="line">	<span class="keyword">while</span> (<span class="built_in">cin</span>&gt;&gt;n&gt;&gt;k)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)</div><div class="line">			<span class="built_in">cin</span>&gt;&gt;a[i];</div><div class="line">		<span class="built_in">cout</span>&lt;&lt;select(<span class="number">0</span>, n<span class="number">-1</span>, k)&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>//半数集问题的递归算法<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;            </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="keyword">int</span> a[<span class="number">1001</span>];</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">comp</span><span class="params">(<span class="keyword">int</span> n)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> ans=<span class="number">1</span>;</div><div class="line">	<span class="keyword">if</span>(a[n]&gt;<span class="number">0</span>)<span class="keyword">return</span> a[n];</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n/<span class="number">2</span>;i++) </div><div class="line">		ans+=comp(i);</div><div class="line">	a[n]=ans;</div><div class="line">	<span class="keyword">return</span> ans;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> n;</div><div class="line">	<span class="keyword">while</span>(<span class="built_in">cin</span>&gt;&gt;n)</div><div class="line">	&#123;</div><div class="line">		<span class="built_in">memset</span>(a,<span class="number">0</span>,<span class="keyword">sizeof</span>(a));</div><div class="line">		a[<span class="number">1</span>]=<span class="number">1</span>;</div><div class="line">		<span class="built_in">cout</span>&lt;&lt;comp(n)&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="分治算法"><a href="#分治算法" class="headerlink" title="分治算法"></a>分治算法</h2><p>选择问题<br>//采用分治策略找出第k小元素的算法<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 1001</span></div><div class="line"><span class="keyword">int</span> a[NUM];</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">select</span><span class="params">(<span class="keyword">int</span> left, <span class="keyword">int</span> right, <span class="keyword">int</span> k)</span></span></div><div class="line"><span class="function"></span>&#123;   </div><div class="line">    <span class="comment">//找到第k小的元素</span></div><div class="line">	<span class="keyword">if</span> (left &gt;= right) <span class="keyword">return</span> a[left];</div><div class="line">	<span class="keyword">int</span> i = left;</div><div class="line">	<span class="keyword">int</span> j = right+<span class="number">1</span>; </div><div class="line">	<span class="comment">//把最左边的元素作为分界数据</span></div><div class="line">	<span class="keyword">int</span> pivot = a[left];</div><div class="line">	<span class="keyword">while</span> (<span class="literal">true</span>) </div><div class="line">	&#123;</div><div class="line">		<span class="keyword">do</span> &#123;</div><div class="line">			i = i+<span class="number">1</span>;</div><div class="line">		&#125; <span class="keyword">while</span> (a[i] &lt; pivot);</div><div class="line">		<span class="keyword">do</span> &#123;</div><div class="line">			j = j<span class="number">-1</span>;</div><div class="line">		&#125; <span class="keyword">while</span> (a[j] &gt; pivot);</div><div class="line">		<span class="keyword">if</span> (i &gt;= j) <span class="keyword">break</span>;</div><div class="line">		swap(a[i], a[j]);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span> (j-left+<span class="number">1</span> == k) <span class="keyword">return</span> pivot;</div><div class="line">	a[left] = a[j];</div><div class="line">	a[j] = pivot;</div><div class="line">	<span class="keyword">if</span> (j-left+<span class="number">1</span> &lt; k)</div><div class="line">        <span class="keyword">return</span> select(j+<span class="number">1</span>,right,k-j+left<span class="number">-1</span>);</div><div class="line">        <span class="keyword">else</span> <span class="keyword">return</span> select(left,j<span class="number">-1</span>,k);</div><div class="line">&#125;</div><div class="line">    选择第K个大的数</div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> *a, <span class="keyword">int</span> *b)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">    <span class="keyword">int</span> tmp;</div><div class="line">    tmp = *a;</div><div class="line">    *a = *b;</div><div class="line">    *b = tmp;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">partition</span><span class="params">(<span class="keyword">int</span> arr[], <span class="keyword">int</span> left, <span class="keyword">int</span> right, <span class="keyword">int</span> pivotIndex)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">    <span class="keyword">int</span> storeIndex = left;</div><div class="line">    <span class="keyword">int</span> pivotValue = arr[pivotIndex];</div><div class="line">    <span class="keyword">int</span> i;</div><div class="line"></div><div class="line">    swap(&amp;arr[pivotIndex],&amp;arr[right]);</div><div class="line">    </div><div class="line">    <span class="keyword">for</span> (i = left; i &lt; right; i ++)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">if</span> (arr[i] &gt; pivotValue)</div><div class="line">        &#123;</div><div class="line">            swap(&amp;arr[i],&amp;arr[storeIndex]);</div><div class="line">            storeIndex++;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    swap(&amp;arr[storeIndex],&amp;arr[right]);</div><div class="line">    <span class="keyword">return</span> storeIndex;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">findKMax</span><span class="params">(<span class="keyword">int</span> arr[], <span class="keyword">int</span> left, <span class="keyword">int</span> right, <span class="keyword">int</span> k)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">    <span class="keyword">int</span> nRet;</div><div class="line">    <span class="keyword">int</span> pivotIndex = left + <span class="number">1</span>;</div><div class="line"></div><div class="line">    nRet = partition(arr,left,right,pivotIndex);</div><div class="line">    <span class="keyword">if</span> (nRet &lt; k)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">return</span> findKMax(arr,nRet+<span class="number">1</span>,right,k);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (nRet &gt; k)</div><div class="line">    &#123;</div><div class="line">        <span class="keyword">return</span> findKMax(arr,left,nRet<span class="number">-1</span>,k);</div><div class="line">    &#125;</div><div class="line">        </div><div class="line">    <span class="keyword">return</span> nRet;</div><div class="line">&#125;</div><div class="line"></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">    <span class="keyword">int</span> i,k,nRet;</div><div class="line">    <span class="keyword">int</span> arr[] = &#123;<span class="number">8</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">1</span>,<span class="number">9</span>,<span class="number">7</span>,<span class="number">6</span>,<span class="number">10</span>&#125;;</div><div class="line"></div><div class="line">    <span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;k);</div><div class="line">    nRet = findKMax(arr,<span class="number">0</span>,<span class="number">7</span>,k<span class="number">-1</span>);</div><div class="line">    </div><div class="line">    <span class="built_in">printf</span>(<span class="string">"The Kth Max Number locate in %d is :%d\n"</span>,nRet,arr[nRet]);</div><div class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i++)</div><div class="line">    &#123;</div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%3d"</span>,arr[i]);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>/循环赛程表<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX 100</span></div><div class="line"></div><div class="line"><span class="keyword">int</span> a[MAX][MAX];</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Copy</span><span class="params">(<span class="keyword">int</span> tox, <span class="keyword">int</span> toy, <span class="keyword">int</span> fromx, <span class="keyword">int</span> fromy, <span class="keyword">int</span> r)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;r; i++)</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;r; j++)  </div><div class="line">			a[tox+i][toy+j] = a[fromx+i][fromy+j];</div><div class="line">&#125;</div><div class="line"><span class="comment">//构造循环赛日程表，选手的数量n=2^k</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Table</span><span class="params">(<span class="keyword">int</span> k)</span></span></div><div class="line"><span class="function"></span>&#123;	</div><div class="line">	<span class="keyword">int</span> i, r;</div><div class="line">	<span class="keyword">int</span> n = <span class="number">1</span> &lt;&lt; k;	</div><div class="line">	<span class="comment">//构造正方形表格的第一行数据</span></div><div class="line">	<span class="keyword">for</span> (i=<span class="number">0</span>; i&lt;n; i++)</div><div class="line">		a[<span class="number">0</span>][i] = i + <span class="number">1</span>;</div><div class="line">		<span class="comment">//采用分治算法构造整个循环日程赛表</span></div><div class="line">	<span class="keyword">for</span> (r=<span class="number">1</span>; r&lt;n; r&lt;&lt;=<span class="number">1</span>)</div><div class="line">		<span class="keyword">for</span> (i=<span class="number">0</span>; i&lt;n; i+=<span class="number">2</span>*r)</div><div class="line">		&#123; </div><div class="line">			Copy(r, i + r, <span class="number">0</span>, i, r);</div><div class="line">			Copy(r, i, <span class="number">0</span>, i + r, r);</div><div class="line">		&#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Out</span><span class="params">(<span class="keyword">int</span> n)</span></span></div><div class="line"><span class="function"></span>&#123; </div><div class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;n; j++) </div><div class="line">			<span class="built_in">printf</span>(<span class="string">"%3d"</span>, a[i][j]);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">	&#125;</div><div class="line">	<span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123; </div><div class="line">	<span class="keyword">int</span> k;</div><div class="line">	<span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;k) &amp;&amp; k)</div><div class="line">	&#123; </div><div class="line">		<span class="keyword">int</span> n = <span class="number">1</span> &lt;&lt; k; </div><div class="line">		Table(k); </div><div class="line">		Out(n);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h2><p>矩阵链乘积<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="keyword">int</span> m[<span class="number">100</span>][<span class="number">100</span>];</div><div class="line"><span class="keyword">int</span> p[<span class="number">100</span>],s[<span class="number">100</span>][<span class="number">100</span>];</div><div class="line"><span class="keyword">int</span> k;</div><div class="line"><span class="function"><span class="keyword">int</span>  <span class="title">TraceBack</span><span class="params">(<span class="keyword">int</span> i,<span class="keyword">int</span> j)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span>(m[i][j]&gt;<span class="number">0</span>) <span class="keyword">return</span> m[i][j];</div><div class="line">        <span class="keyword">if</span>(i==j) <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">        <span class="keyword">int</span> u=TraceBack(i,i)+TraceBack(i+<span class="number">1</span>,j)+p[i<span class="number">-1</span>]*p[k]*p[j];</div><div class="line">        s[i][j]=i;</div><div class="line">        <span class="keyword">for</span>(<span class="keyword">int</span> k=i+<span class="number">1</span>; k&lt;j; k++) &#123;</div><div class="line">                <span class="keyword">int</span> t=TraceBack(i,k)+TraceBack(k+<span class="number">1</span>,j)+p[i<span class="number">-1</span>]*p[k]*p[j];</div><div class="line">                <span class="keyword">if</span>(t&lt;u) &#123;</div><div class="line">                        u=t;</div><div class="line">                        s[i][j]=k;</div><div class="line">                &#125;</div><div class="line">                m[i][j]=u;</div><div class="line">                <span class="keyword">return</span> u;</div><div class="line">        &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>最多单减子序列<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> MAX 1000</span></div><div class="line"><span class="keyword">int</span> a[MAX] = &#123;<span class="number">2</span>,<span class="number">1</span>,<span class="number">3</span>&#125;;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">select</span><span class="params">(<span class="keyword">int</span> left,<span class="keyword">int</span> right,<span class="keyword">int</span> k)</span> </span>&#123;</div><div class="line">	<span class="keyword">if</span>(left&gt;=right) <span class="keyword">return</span> a[left];</div><div class="line">	<span class="keyword">int</span> i =left;</div><div class="line">	<span class="keyword">int</span> j = right+<span class="number">1</span>;</div><div class="line">	<span class="keyword">int</span> pivot = a[left]; <span class="comment">//把第一个设为界点</span></div><div class="line">	<span class="keyword">while</span>(<span class="literal">true</span>) &#123;</div><div class="line">		<span class="keyword">do</span> &#123;</div><div class="line">			i++;</div><div class="line">		&#125; <span class="keyword">while</span>(a[i]&lt;pivot);</div><div class="line">		<span class="keyword">do</span> &#123;</div><div class="line">			j--;</div><div class="line">		&#125; <span class="keyword">while</span>(a[j]&gt;pivot);</div><div class="line">		<span class="keyword">if</span>(i&gt;=j) <span class="keyword">break</span>;</div><div class="line">		swap(a[i],a[j]);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">if</span>(j-left+<span class="number">1</span>==k) <span class="keyword">return</span> pivot; <span class="comment">//该界点就是要找的数</span></div><div class="line">	a[left]=a[j];</div><div class="line">	a[j]=pivot;</div><div class="line">	<span class="keyword">if</span>(j-left+<span class="number">1</span>&lt;k) <span class="keyword">return</span> select(j+<span class="number">1</span>, right, k-j+left<span class="number">-1</span>);</div><div class="line">	<span class="keyword">else</span> <span class="keyword">return</span> select(left,j<span class="number">-1</span>,k);</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line"><span class="comment">//	int a[3] = &#123;2,1,3&#125;;</span></div><div class="line">	<span class="built_in">cout</span>&lt;&lt;select(<span class="number">0</span>,<span class="number">2</span>,<span class="number">2</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>//计算0-1背包的动态规划算法<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;ctime&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 50</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> CAP 1500</span></div><div class="line"><span class="keyword">int</span> v[NUM];</div><div class="line"><span class="keyword">int</span> w[NUM];</div><div class="line"><span class="keyword">int</span> p[NUM][CAP];</div><div class="line"><span class="comment">//形参c是背包的容量w，n是物品的数量</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">knapsack</span><span class="params">(<span class="keyword">int</span> c, <span class="keyword">int</span> n)</span> </span></div><div class="line"><span class="function"></span>&#123; </div><div class="line">	<span class="keyword">int</span> jMax=min(w[n]<span class="number">-1</span>,c); </div><div class="line">	<span class="keyword">for</span>( <span class="keyword">int</span> j=<span class="number">0</span>; j&lt;=jMax; j++) </div><div class="line">		p[n][j]=<span class="number">0</span>; </div><div class="line">	<span class="keyword">for</span>( <span class="keyword">int</span> j=w[n]; j&lt;=c; j++) </div><div class="line">		p[n][j]=v[n];</div><div class="line">	<span class="keyword">for</span>( <span class="keyword">int</span> i=n<span class="number">-1</span>; i&gt;<span class="number">1</span>; i--) </div><div class="line">	&#123; </div><div class="line">		jMax=min(w[i]<span class="number">-1</span>,c); </div><div class="line">		<span class="keyword">for</span>( <span class="keyword">int</span> j=<span class="number">0</span>; j&lt;=jMax; j++) </div><div class="line">			p[i][j]=p[i+<span class="number">1</span>][j]; </div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=w[i]; j&lt;=c; j++) </div><div class="line">			p[i][j]=max(p[i+<span class="number">1</span>][j], p[i+<span class="number">1</span>][j-w[i]]+v[i]); </div><div class="line">	&#125; </div><div class="line">	p[<span class="number">1</span>][c]=p[<span class="number">2</span>][c]; </div><div class="line">	<span class="keyword">if</span> (c&gt;=w[<span class="number">1</span>]) </div><div class="line">		p[<span class="number">1</span>][c]=max(p[<span class="number">1</span>][c], p[<span class="number">2</span>][c-w[<span class="number">1</span>]]+v[<span class="number">1</span>]); </div><div class="line">&#125; </div><div class="line"></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">traceback</span><span class="params">( <span class="keyword">int</span> c, <span class="keyword">int</span> n, <span class="keyword">int</span> x[ ])</span> </span></div><div class="line"><span class="function"></span>&#123; </div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;n; i++) </div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span> (p[i][c]==p[i+<span class="number">1</span>][c]) x[i]=<span class="number">0</span>; </div><div class="line">		<span class="keyword">else</span> &#123; x[i]=<span class="number">1</span>; c-=w[i]; &#125; </div><div class="line">	&#125;</div><div class="line">	x[n]=(p[n][c])? <span class="number">1</span>:<span class="number">0</span>; </div><div class="line">&#125; </div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span> <span class="params">()</span> </span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> x[NUM];</div><div class="line">	<span class="keyword">int</span> W;</div><div class="line">	<span class="keyword">int</span> n;</div><div class="line">	<span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;W) &amp;&amp; W) </div><div class="line">	&#123;</div><div class="line">		<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=n; i++)</div><div class="line">			<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;w[i], &amp;v[i]);</div><div class="line">		<span class="built_in">memset</span> (p, <span class="number">0</span>, <span class="keyword">sizeof</span>(p));</div><div class="line">		knapsack(W, n);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%d\n"</span>, p[<span class="number">1</span>][W]);</div><div class="line">		traceback(W, n, x);</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=n; i++)</div><div class="line">			<span class="keyword">if</span> (x[i]) <span class="built_in">printf</span>(<span class="string">"%d "</span>, i);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"\n"</span>);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>//计算最大字段和的动态规划算法的最优解<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 1001</span></div><div class="line"><span class="keyword">int</span> a[NUM];</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">MaxSum</span><span class="params">(<span class="keyword">int</span> n, <span class="keyword">int</span> &amp;besti, <span class="keyword">int</span> &amp;bestj)</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> sum=<span class="number">0</span>; </div><div class="line">	<span class="keyword">int</span> b=<span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> begin = <span class="number">0</span>;</div><div class="line">	<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=n;i++)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">if</span> (b&gt;<span class="number">0</span>) b+=a[i]; </div><div class="line">		<span class="keyword">else</span> &#123;b=a[i];begin = i;&#125;</div><div class="line">		<span class="keyword">if</span> (b&gt;sum) </div><div class="line">		&#123;</div><div class="line">			sum = b; </div><div class="line">			besti = begin; </div><div class="line">			bestj = i;</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> sum;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">int</span> n;</div><div class="line">	<span class="keyword">int</span> besti, bestj;</div><div class="line">	<span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n) &amp;&amp; n) </div><div class="line">	&#123;</div><div class="line">		besti = <span class="number">0</span>;</div><div class="line">		bestj = <span class="number">0</span>;</div><div class="line">		<span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=n; i++)</div><div class="line">			<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;a[i]);</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"%d\n"</span>, MaxSum(n, besti, bestj));</div><div class="line">		<span class="built_in">printf</span>(<span class="string">"From %d to %d\n"</span>, besti, bestj);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="贪心算法"><a href="#贪心算法" class="headerlink" title="贪心算法"></a>贪心算法</h2><p>//删数问题 ，找最近下降点<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;String&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">	<span class="built_in">string</span> a;</div><div class="line">	<span class="keyword">int</span> k;</div><div class="line">	<span class="built_in">cin</span>&gt;&gt;a&gt;&gt;k;</div><div class="line">	<span class="keyword">if</span>(k&gt;=a.size()) a.erase();</div><div class="line">	<span class="keyword">else</span> <span class="keyword">while</span>(k&gt;<span class="number">0</span>) &#123;</div><div class="line">		<span class="keyword">int</span> i;</div><div class="line">		<span class="keyword">for</span>(i=<span class="number">0</span>; (i&lt;a.size()<span class="number">-1</span>)&amp;&amp;a[i]&lt;=a[i+<span class="number">1</span>]; i++);</div><div class="line">		a.erase(i,<span class="number">1</span>);</div><div class="line">		k--;</div><div class="line">		&#125;</div><div class="line">	<span class="keyword">while</span>(a.size()&gt;<span class="number">1</span> &amp;&amp; a[<span class="number">0</span>]==<span class="string">'0'</span>)</div><div class="line">		a.erase(<span class="number">0</span>,<span class="number">1</span>);</div><div class="line">	<span class="built_in">cout</span>&lt;&lt;a&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>删数问题<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">   <span class="built_in">string</span> n;</div><div class="line">   <span class="keyword">int</span> s,i,x,l,m;</div><div class="line">   <span class="keyword">while</span>(<span class="built_in">cin</span>&gt;&gt;n&gt;&gt;s)</div><div class="line">   &#123;</div><div class="line">        i=<span class="number">-1</span>,m=<span class="number">0</span>,x=<span class="number">0</span>;</div><div class="line">        l=n.length(); </div><div class="line">        <span class="keyword">while</span>(x&lt;s&amp;&amp;m==<span class="number">0</span>)</div><div class="line">         &#123;</div><div class="line">             i++;</div><div class="line">             <span class="keyword">if</span>(n[i]&gt;n[i+<span class="number">1</span>])<span class="comment">//出现递减,删除递减的首数字 </span></div><div class="line">              &#123;</div><div class="line">                  n=n.erase(i,<span class="number">1</span>);</div><div class="line">                  x++;<span class="comment">// x统计删除数字的个数 </span></div><div class="line">                  i=<span class="number">-1</span>;<span class="comment">//从头开始查递减区间 </span></div><div class="line">              &#125; </div><div class="line">             <span class="keyword">if</span>(i==l-x<span class="number">-2</span>&amp;&amp;x&lt;s)</div><div class="line">             m=<span class="number">1</span>;<span class="comment">//已经无递减区间,m=1脱离循环 </span></div><div class="line">         &#125; </div><div class="line">     <span class="built_in">cout</span>&lt;&lt;n.substr(<span class="number">0</span>,l-s+x)&lt;&lt;<span class="built_in">endl</span>;<span class="comment">//只打印剩下的左边l-(s-x)个数字 </span></div><div class="line">   &#125; </div><div class="line">   <span class="keyword">return</span> <span class="number">0</span>;   </div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="回溯算法"><a href="#回溯算法" class="headerlink" title="回溯算法"></a>回溯算法</h2><p>轮船装载（回溯）<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 100             <span class="comment">//解空间树 </span></span></div><div class="line"><span class="keyword">int</span> n;     <span class="comment">//集装箱数量</span></div><div class="line"><span class="keyword">int</span> c;     <span class="comment">//轮船装在数量</span></div><div class="line"><span class="keyword">int</span> w[NUM];<span class="comment">//集装箱重量数组</span></div><div class="line"><span class="keyword">int</span> x[NUM];<span class="comment">//解向量</span></div><div class="line"><span class="keyword">int</span> r;     <span class="comment">//剩余集装箱重量</span></div><div class="line"><span class="keyword">int</span> cw;    <span class="comment">//当前轮船载重量</span></div><div class="line"><span class="keyword">int</span> bestw; <span class="comment">//当前最优载重量</span></div><div class="line"><span class="keyword">int</span> bestx[NUM];<span class="comment">//当前最优解</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Backtrack</span><span class="params">(<span class="keyword">int</span> t)</span> </span>&#123;</div><div class="line">	<span class="comment">//到达叶子节点</span></div><div class="line">	<span class="keyword">if</span>(t&gt;n) &#123;</div><div class="line">		<span class="keyword">if</span>(cw&gt;bestw) &#123;    <span class="comment">//更新最优解</span></div><div class="line">			<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=n; i++)</div><div class="line">				bestx[i] = x[i];</div><div class="line">			bestw = cw;</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">return</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="comment">//更新剩余集装箱的重量</span></div><div class="line">	r-= w[t];</div><div class="line">	<span class="comment">//搜索左子树</span></div><div class="line">	<span class="keyword">if</span>(cw+w[t]&lt;=c) &#123;</div><div class="line">		x[t]=<span class="number">1</span>;</div><div class="line">		\</div><div class="line">		cw+=w[t];</div><div class="line">		Backtrack(t+<span class="number">1</span>);</div><div class="line">		cw-=w[t];<span class="comment">//恢复 状态</span></div><div class="line">	&#125;</div><div class="line"></div><div class="line">	<span class="comment">//搜索右子树</span></div><div class="line">	<span class="keyword">if</span>(cw+r&gt;bestw) &#123;</div><div class="line">		x[t]=<span class="number">0</span>;</div><div class="line">		Backtrack(t+<span class="number">1</span>);</div><div class="line">	&#125;</div><div class="line"></div><div class="line">	r+= w[t];<span class="comment">//恢复状态</span></div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>N皇后问题<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 20</span></div><div class="line"><span class="keyword">int</span> n=<span class="number">8</span>;</div><div class="line"><span class="keyword">int</span> x[NUM]; <span class="comment">//记录解向量,列号</span></div><div class="line"><span class="keyword">int</span> sum=<span class="number">0</span>; <span class="comment">//解的个数</span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">bool</span> <span class="title">Place</span><span class="params">(<span class="keyword">int</span> t)</span> </span>&#123;</div><div class="line">	<span class="keyword">int</span> i;</div><div class="line">	<span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;t; i++) &#123;</div><div class="line">		<span class="keyword">if</span>((<span class="built_in">abs</span>(t-i) == <span class="built_in">abs</span>(x[i]-x[t])) || (x[i]==x[t]))</div><div class="line">			<span class="keyword">return</span> <span class="literal">false</span>;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="literal">true</span>;  <span class="comment">//只要不满足循环就返回true</span></div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Backtrack</span><span class="params">(<span class="keyword">int</span> t)</span> </span>&#123;</div><div class="line">	<span class="keyword">int</span> i;<span class="comment">//叶子节点</span></div><div class="line">	<span class="keyword">if</span>(t&gt;n)	&#123;</div><div class="line">		sum++;</div><div class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;=n; i++) &#123;</div><div class="line">			<span class="built_in">cout</span>&lt;&lt;x[i];</div><div class="line">		&#125;</div><div class="line">		<span class="built_in">cout</span>&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">	&#125; <span class="keyword">else</span> &#123;</div><div class="line">		<span class="keyword">for</span>(i=<span class="number">1</span>; i&lt;=n; i++) &#123;</div><div class="line">			x[t]=i;</div><div class="line">			<span class="keyword">if</span>(Place(t)) Backtrack(t+<span class="number">1</span>);</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">&#125;</div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</div><div class="line">	<span class="comment">//cin&gt;&gt;n;</span></div><div class="line">	Backtrack(<span class="number">1</span>);</div><div class="line">	<span class="built_in">cout</span>&lt;&lt;<span class="string">"Total="</span>&lt;&lt;sum&lt;&lt;<span class="built_in">endl</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<hr>
<h2 id="分支限界算法"><a href="#分支限界算法" class="headerlink" title="分支限界算法"></a>分支限界算法</h2><p>装载问题分支限界算法的实现<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;queue&gt;</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;algorithm&gt;</span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> NUM 100</span></div><div class="line"><span class="keyword">int</span> n;<span class="comment">//集装箱的数量</span></div><div class="line"><span class="keyword">int</span> c;<span class="comment">//轮船的载重量</span></div><div class="line"><span class="keyword">int</span> w[NUM];<span class="comment">////集装箱的重量数组</span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">MaxLoading</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; Q;</div><div class="line">	Q.push(<span class="number">-1</span>);</div><div class="line">	<span class="keyword">int</span> i = <span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> Ew = <span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> bestw = <span class="number">0</span>;</div><div class="line">	<span class="keyword">int</span> r = <span class="number">0</span>;</div><div class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">1</span>; j&lt;n; j++)</div><div class="line">		r += w[j];</div><div class="line">	<span class="keyword">while</span> (<span class="literal">true</span>)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">int</span> wt = Ew+w[i];</div><div class="line">		<span class="keyword">if</span> (wt&lt;=c)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">if</span> (wt&gt;bestw) bestw = wt;</div><div class="line">			<span class="keyword">if</span> (i&lt;n<span class="number">-1</span>) Q.push(wt);</div><div class="line">		&#125;</div><div class="line">		<span class="keyword">if</span> (Ew+r&gt;bestw &amp;&amp; i&lt;n<span class="number">-1</span>) Q.push(Ew);</div><div class="line">		Ew = Q.front();</div><div class="line">		Q.pop();</div><div class="line">		<span class="keyword">if</span> (Ew==<span class="number">-1</span>)</div><div class="line">		&#123;</div><div class="line">			<span class="keyword">if</span> (Q.empty()) <span class="keyword">return</span> bestw;</div><div class="line">			Q.push(<span class="number">-1</span>);</div><div class="line">			Ew = Q.front();</div><div class="line">			Q.pop();</div><div class="line">			i++;</div><div class="line">			r -= w[i];</div><div class="line">		&#125;</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> bestw;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></div><div class="line"><span class="function"></span>&#123;</div><div class="line">	<span class="keyword">while</span>(<span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;c, &amp;n)!=EOF)</div><div class="line">	&#123;</div><div class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)</div><div class="line">			<span class="built_in">scanf</span>(<span class="string">"%d"</span>,&amp;w[i]);</div><div class="line">		<span class="keyword">int</span> ans  =  MaxLoading();</div><div class="line">		<span class="keyword">if</span> (ans) <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);</div><div class="line">		<span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"No answer!\n"</span>);</div><div class="line">	&#125;</div><div class="line">	<span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>请我吃辣条吧！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/pay/wx.png" alt="lewyu WeChat Pay"/>
        <p>WeChat Pay</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/pay/zfb.png" alt="lewyu Alipay"/>
        <p>Alipay</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/10/08/褐色鸟群/" rel="next" title="LOST&FOUND">
                <i class="fa fa-chevron-left"></i> LOST&FOUND
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/10/27/NASA/" rel="prev" title="NASA">
                NASA <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/img/touxiang.gif"
               alt="lewyu" />
          <p class="site-author-name" itemprop="name">lewyu</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/lewyu" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                    
                      GitHub
                    
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://twitter.com/lewyu521" target="_blank" title="Twitter">
                  
                    <i class="fa fa-fw fa-twitter"></i>
                  
                    
                      Twitter
                    
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/lewyu521" target="_blank" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                    
                      微博
                    
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/lewyu" target="_blank" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                    
                      知乎
                    
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#蛮力算法"><span class="nav-number">1.</span> <span class="nav-text">蛮力算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#递归算法"><span class="nav-number">2.</span> <span class="nav-text">递归算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分治算法"><span class="nav-number">3.</span> <span class="nav-text">分治算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#动态规划"><span class="nav-number">4.</span> <span class="nav-text">动态规划</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#贪心算法"><span class="nav-number">5.</span> <span class="nav-text">贪心算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#回溯算法"><span class="nav-number">6.</span> <span class="nav-text">回溯算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#分支限界算法"><span class="nav-number">7.</span> <span class="nav-text">分支限界算法</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">lewyu</span>
</div>

<br>
<footer class="footer">

    <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>

</footer>
        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.2"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  

  

  

</body>
</html>
